home *** CD-ROM | disk | FTP | other *** search
- Path: portal.gmu.edu!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: returning address of value to main
- Date: 11 Apr 96 12:01:32 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.829224092@rscernix>
- References: <4khpjq$a3c@pipe1.nyc.pipeline.com>
- NNTP-Posting-Host: ues5.cern.ch
- Mime-Version: 1.0
- Content-Type: text/plain; charset=US-ASCII
- Content-Transfer-Encoding: 7bit
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <4khpjq$a3c@pipe1.nyc.pipeline.com> luciferm@nyc.pipeline.com (manjila thapa) writes:
-
- >I want to return the address of element of an array
- >a[0]....a[n]....a[m-1] (address of a[n] is to be returned)
- >from a function to the main so that:
- >
- >main(){
- >double *newarr;
- >...
- >...
- >newarr= function(......) /*function is supposed to return address*/
- >__________________________________________
- >i tried
- >
- >return &a[n];
- >
- >in function but it doesn't work! Isn't this the right way to get the
- >address? (this is part of my hw, hope u don't mind)
-
- No, we don't mind helping people doing their homeworks, as long as they
- don't ask us to do them for themselves :-)
-
- Your return statement is correct IF 'a' is an array of double which is
- not declared with automatic storage in function(), i.e. if its storage
- will continue to be allocated after the function returns (even if the
- array itself goes out of scope).
-
- So, these examples are correct:
-
- double a[SIZE];
- double *function()
- {
- ...
- return &a[n];
- }
- -------------------------
- double *function()
- {
- static double a[SIZE];
- ...
- return &a[n];
- }
-
- but this isn't:
-
- double *function()
- {
- double a[SIZE];
- ...
- return &a[n];
- }
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-